﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Linq;
using RStein.PosterousReader.WP.ViewModels.ViewObjects;
using RStein.Posterous.API;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace RStein.PosterousReader.WP.HostServices
{
  internal class DefaultWPNavigationService : INavigationService
  {
    public static readonly char QUERY_STRING_SEPARATOR = '&';
    public static readonly char QUERY_BEGIN_STRING_SEPARATOR = '?';
    public static readonly char QUERY_KEY_VALUE_SEPARATOR = '=';
    private PhoneApplicationFrame m_navigationService;
    private readonly string POST_DETAIL_PAGE_FORMAT = "/Views/PostDetailView.xaml?id={0}";

    public DefaultWPNavigationService(PhoneApplicationFrame navigationService)
    {
      if (navigationService == null)
      {
        throw new ArgumentNullException("navigationService");

      }

      m_navigationService = navigationService;
    }

    public virtual void Navigate(Uri uri)
    {

      DispatcherSynchronizationContext.Current.Send((cb) => m_navigationService.Navigate(uri), null);
    }




    public virtual Uri GetDetailUri(object obj)
    {
      //TODO : More flexible implementation
      var post = obj as IPosterousPost;

      if (post == null)
      {
        return null;
      }

      var uri = new Uri(String.Format(POST_DETAIL_PAGE_FORMAT, post.Id), UriKind.Relative);
      return uri;
    }


    public virtual string GetQueryParameter(string key)
    {

      string query = null;
      DispatcherSynchronizationContext.Current.Send(cb => query = m_navigationService.CurrentSource.ToString(), null);

      var queryStrings = query.Split(QUERY_BEGIN_STRING_SEPARATOR).Last();
      return (from keyValue in queryStrings.Split(QUERY_STRING_SEPARATOR)
              where keyValue.StartsWith(key, StringComparison.OrdinalIgnoreCase)
              let keyValues = keyValue.Split(QUERY_KEY_VALUE_SEPARATOR)
              select keyValues.Last()).FirstOrDefault();


    }


    public void GoBack()
    {
      DispatcherSynchronizationContext.Current.Send(cb =>
                                                        {
                                                          if (m_navigationService.CanGoBack)
                                                          {
                                                            m_navigationService.GoBack();
                                                          }
                                                        }, null);

    }


    public void GoForward()
    {
      DispatcherSynchronizationContext.Current.Send(cb =>
                                                        {
                                                          if (m_navigationService.CanGoForward)
                                                          {
                                                            m_navigationService.GoForward();
                                                          }
                                                        }, null);
    }
  }
}
